use std::io::{BufWriter, stdout, stdin};
use rand::Rng;
use std::cmp::Ordering;
use wasm_bindgen::prelude::*;
use std::any::Any;
mod one;
mod http;

// wasm编译
#[wasm_bindgen]
pub fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

// 判断是否为 u32
fn is_u32(value: &dyn Any) -> bool {
    // 检查值是否为 u32 类型
    value.is::<u32>()
}
// 是否为 a-z
fn is_letter(c: char) -> bool {
    c.is_ascii_alphabetic()
}

fn endingFn (num:u32) {
  let mut randomString = String::new();
  // io 35
  // stdin().read_line(&mut randomString).expect("无法读取行信息");
  stdin().read_line(&mut randomString).expect("无法读取行信息");
  println!("输入的是：{}", randomString);
  
  
  let closure= &endingFn;
  // if randomString.trim().is_empty() {
  //   // 为空时候的 处理
  //   println!("KONO IS EMPTY");
  //   closure(num)
  // }else 
  if randomString.trim() == "get" {
    http::http_get();
  }else if randomString.trim() == "post"{
    let oneResult = one::oneFn();
    http::http_post(oneResult);
  }
  else if(randomString.trim() == "oneResult"){
    let oneResult = one::oneFn();
    closure(num)
  }else if !randomString.trim().parse::<u32>().is_ok(){
    println!("请输入 u32类型值");
    closure(num)
  }else {
    let guess_to_string:u32 = randomString.trim().parse().expect("cccccccccccccccccccccccccccc");
    match guess_to_string.cmp(&num) {
      Ordering::Less => {
        println!("TISAI");
        closure(num);
      },
      Ordering::Greater => {
        println!("OOKI");
        closure(num);
      },
      Ordering::Equal => {
        println!("OWARIMASITA");
      },
    };
  };
}


fn main() {
  let secret_number = rand::thread_rng().gen_range(1..100);
  println!("random NUMBER {}",secret_number);

  endingFn(secret_number);
}


